<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
    :cancelText="$t('关闭')"
  >
    <template slot="footer">
      <a-button type="primary" @click="handleOk">{{$t('已确认，请继续盘点')}}</a-button>
      <a-button @click="handleCancel">{{$t('返回盘点单修改')}}</a-button>
    </template>
    <a-form>
      <div>
        <div style="font-weight: bold; color: red; margin-bottom: 10px">
          {{$t('本次盘点存在差异商品，请确认如下差异商品盘点结果是否正确')}}
        </div>
        <a-table
          :columns="columns"
          :dataSource="dataSource"
          size="middle"
          bordered
          class="j-table-force-nowrap"
          :pagination="false"
          style="padding-left: 11px"
        >
          <template slot="error" slot-scope="text, record">
            <span v-if="record.error < 0" style="color: red">{{ record.error }}</span>
            <span v-if="record.error > 0" style="color: green">{{ record.error }}</span>
          </template>
        </a-table>
      </div>
    </a-form>
  </j-modal>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
let _self
export default {
  name: 'GoodsInventorySubmit',
  components: {},
  data() {
    return {
      title: this.$t('差异商品确认'),
      width: 1100,
      visible: false,
      disableSubmit: false,
      columns: [
        {
          title: this.$t('卖品编码'),
          align: 'center',
          dataIndex: 'goodsCode',
        },
        {
          title: this.$t('卖品名称'),
          dataIndex: 'goodsId_dictText',
          customHeaderCell: function (column) {
            return {
              style: {
                textAlign: 'center',
              },
            }
          },
          customCell: function (record, rowIndex) {
            return {
              style: {
                textAlign: 'left',
              },
            }
          },
        },
        {
          title: this.$t('系统库存数量'),
          key: 'systemStock',
          dataIndex: 'systemStock',
          customHeaderCell: function (column) {
            return {
              style: {
                textAlign: 'center',
              },
            }
          },
          customCell: function (record, rowIndex) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('实盘库存数量'),
          key: 'actualStock',
          dataIndex: 'actualStock',
          customHeaderCell: function (column) {
            return {
              style: {
                textAlign: 'center',
              },
            }
          },
          customCell: function (record, rowIndex) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('差异'),
          key: 'error',
          dataIndex: 'error',
          customHeaderCell: function (column) {
            return {
              style: {
                textAlign: 'center',
              },
            }
          },
          customCell: function (record, rowIndex) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          scopedSlots: { customRender: 'error' },
        },
      ],
      dataSource: [],
      model: {},
      store: '',
      goodsId: '',
    }
  },
  created() {
    _self = this
  },
  methods: {
    add() {
      this.visible = true
      this.edit()
    },
    edit(record, model) {
      this.visible = true
      this.dataSource = []
      this.model = model
      record.forEach((col) => {
        if (col.error != 0) {
          this.dataSource.push(col)
        }
      })
    },
    handleOk() {
      let that = this
      let query = {}
      query = that.model
      query.bisGoodsInventoryDetailsList = this.dataSource
      query.state = '1'
      this.$confirm({
        title: this.$t('提示'),
        content: this.$t('盘点操作将按本次的实盘数量更新系统库存，您确定要执行盘点吗？'),
        okText: that.$t("确定"),
        cancelText: that.$t("取消"),
        onOk() {
          let method = ''
          let url = ''
          if (that.model.id) {
            method = 'put'
            url = '/goods/bisGoodsInventory/edit'
          } else {
            method = 'post'
            url = '/goods/bisGoodsInventory/add'
          }
          httpAction(url, query, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.visible = false
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          })
        },
        onCancel() {},
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    submitCallback() {
      this.$emit('ok')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>